package com.pt25.service.sys.repository.mysql.persistence.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pt25.base.dep.db.DbConstants;
import com.pt25.base.util.CollectionUtil;
import com.pt25.service.sys.domain.admin.ddo.AdminPermission;
import com.pt25.service.sys.repository.mysql.converter.AdminConverter;
import com.pt25.service.sys.repository.mysql.mapper.AdminPermissionMapper;
import com.pt25.service.sys.repository.mysql.persistence.AdminPermissionRepo;
import com.pt25.service.sys.repository.mysql.po.AdminPermissionPo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@Service
public class AdminPermissionRepoImpl extends ServiceImpl<AdminPermissionMapper, AdminPermissionPo> implements AdminPermissionRepo {
    @Override
    public AdminPermission getAdminPermission(Long id) {
        return AdminConverter.adminPermissionPO2AdminPermission(getById(id));
    }

    @Override
    public List<AdminPermission> getAdminPermissionsByIds(Set<Long> ids) {
        if (CollectionUtil.isNotEmpty(ids)) {
            QueryWrapper<AdminPermissionPo> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().in(AdminPermissionPo::getId, ids);
            queryWrapper.lambda().eq(AdminPermissionPo::getDelFlag, DbConstants.DEL_FLAG0);
            List<AdminPermission> adminPermissions = new ArrayList<>();
            list(queryWrapper).forEach(a -> adminPermissions.add(AdminConverter.adminPermissionPO2AdminPermission(a)));
            return adminPermissions;
        }
        return Collections.EMPTY_LIST;
    }

    @Override
    public AdminPermission getAdminPermissionByPermissionOrTitle(AdminPermissionPo adminPermissionPO) {
        QueryWrapper<AdminPermissionPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().nested(
                        wq -> wq.eq(AdminPermissionPo::getPermission, adminPermissionPO.getPermission())
                                .or()
                                .eq(AdminPermissionPo::getTitle, adminPermissionPO.getTitle()))
                .eq(AdminPermissionPo::getDelFlag, DbConstants.DEL_FLAG0);
        AdminPermissionPo adminPermissionPoQuery = getOne(queryWrapper);
        return adminPermissionPoQuery != null ? AdminConverter.adminPermissionPO2AdminPermission(adminPermissionPoQuery) : null;
    }

    @Override
    public List<AdminPermission> listAdminPermission() {
        List<AdminPermission> adminPermissions = new ArrayList<>();
        QueryWrapper<AdminPermissionPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(AdminPermissionPo::getId,
                AdminPermissionPo::getParentId,
                AdminPermissionPo::getPermission,
                AdminPermissionPo::getTitle,
                AdminPermissionPo::getSort,
                AdminPermissionPo::getLevel,
                AdminPermissionPo::getUpdateTime);
        queryWrapper.lambda().eq(AdminPermissionPo::getDelFlag, DbConstants.DEL_FLAG0);
        queryWrapper.lambda().orderByDesc(AdminPermissionPo::getSort);
        list(queryWrapper).forEach(a -> adminPermissions.add(AdminConverter.adminPermissionPO2AdminPermission(a)));
        return adminPermissions;
    }

    @Override
    public List<AdminPermission> listAdminPermissionByParentId(Long id) {
        List<AdminPermission> adminPermissions = new ArrayList<>();
        QueryWrapper<AdminPermissionPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AdminPermissionPo::getParentId, id);
        list(queryWrapper).forEach(a -> adminPermissions.add(AdminConverter.adminPermissionPO2AdminPermission(a)));
        return adminPermissions;
    }

    @Override
    public void removePermissionByPermissionIds(Set<Long> ids) {
        removeBatchByIds(ids);
    }
}
